// this function should convert a number from one base to another
// the input number is a string representation of the number
// the first input is likely "9(10)" where 10 is the base of the number
// and the second input is the base to convert to
pub fn convert_base(num_str: &str, to_base: u32) -> String {
    let raw_num = num_str.split('(').collect::<Vec<&str>>()[0];
    let from_base = num_str.split('(').collect::<Vec<&str>>()[1].split(')').collect::<Vec<&str>>()[0].parse::<u32>().unwrap();

    let num = convert_from_base_to_10(raw_num, from_base);
    convert_from_10_to_newbase(num, to_base)
}

fn convert_from_base_to_10(num: &str, from_base: u32) -> u32 {
    let mut result = 0;
    for (i, c) in num.chars().rev().enumerate() {
        let digit = c.to_digit(from_base).unwrap();
        result += digit * from_base.pow(i as u32);
    }
    result
}

// this function should convert a number from base 10 to a new base
fn convert_from_10_to_newbase(num: u32, to_base: u32) -> String {
    let mut result = String::new();
    let mut num = num;
    while num > 0 {
        let digit = num % to_base;
        if digit > 9 {
            let c = (digit - 10 + 'a' as u32) as u8 as char;
            result = format!("{}{}", c, result);
        } else {
            result = format!("{}{}", digit, result);
        }
        num /= to_base;
    }
    result
}
